package com.glory.gloryquickui.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * FileName: SharePreferencesUtils
 * Author: XJH
 * Date: 2021/6/3 9:49
 * Description:
 * History:
 */
public class SharePreferencesUtils {
    public SharePreferencesUtils() {
    }

    public static void setSharePre(String sName, String sKey, String sValue, Context context) {
        SharedPreferences s = context.getSharedPreferences(sName, 0);
        SharedPreferences.Editor editor = s.edit();
        editor.putString(sKey, sValue);
        editor.commit();
    }

    public static void setSharePre(String sName, String sKey, float sValue, Context context) {
        SharedPreferences s = context.getSharedPreferences(sName, 0);
        SharedPreferences.Editor editor = s.edit();
        editor.putFloat(sKey, sValue);
        editor.commit();
    }

    public static String getSharePre(String sName, String sKey, String defValue, Context context) {
        SharedPreferences s = context.getSharedPreferences(sName, 0);
        String str = s.getString(sKey, defValue);
        return str;
    }

    public static String getSharePre(String sName, String sKey, Context context) {
        SharedPreferences s = context.getSharedPreferences(sName, 0);
        String str = s.getString(sKey, "");
        return str;
    }

    public static float getSharePreFloat(String sName, String sKey, Context context) {
        SharedPreferences s = context.getSharedPreferences(sName, 0);
        float str = s.getFloat(sKey, 0.0F);
        return str;
    }

    public static long getSharePreLong(String sName, String sKey, Context context) {
        SharedPreferences s = context.getSharedPreferences(sName, 0);
        long str = s.getLong(sKey, 0L);
        return str;
    }

    public static boolean getSharePreBoolean(String sName, String key, Context context) {
        SharedPreferences s = context.getSharedPreferences(sName, 0);
        return s.getBoolean(key, false);
    }

    public static boolean getSharePreBoolean(String sName, String key, boolean defaultI, Context context) {
        SharedPreferences s = context.getSharedPreferences(sName, 0);
        return s.getBoolean(key, defaultI);
    }

    public static int getSharePreInt(String sName, String sKey, Context context) {
        SharedPreferences s = context.getSharedPreferences(sName, 0);
        int str = s.getInt(sKey, -1);
        return str;
    }

    public static void setSharePre(String sName, String sKey, boolean sValue, Context context) {
        SharedPreferences s = context.getSharedPreferences(sName, 0);
        SharedPreferences.Editor editor = s.edit();
        editor.putBoolean(sKey, sValue);
        editor.commit();
    }

    public static void setSharePre(String sName, String sKey, int sValue, Context context) {
        SharedPreferences s = context.getSharedPreferences(sName, 0);
        SharedPreferences.Editor editor = s.edit();
        editor.putInt(sKey, sValue);
        editor.commit();
    }

    public static void setSharePre(String sName, String sKey, long sValue, Context context) {
        SharedPreferences s = context.getSharedPreferences(sName, 0);
        SharedPreferences.Editor editor = s.edit();
        editor.putLong(sKey, sValue);
        editor.commit();
    }

    public static void saveSerializableObject(Context context, String fileName, String key, Object object) throws IOException {
        SharedPreferences.Editor spEdit = context.getSharedPreferences(fileName, 0).edit();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(bos);
        os.writeObject(object);
        String bytesToHexString = bytesToHexString(bos.toByteArray());
        spEdit.putString(key, bytesToHexString);
        spEdit.commit();
    }

    public static String bytesToHexString(byte[] bArray) {
        if (bArray == null) {
            return null;
        } else if (bArray.length == 0) {
            return "";
        } else {
            StringBuffer sb = new StringBuffer(bArray.length);

            for (int i = 0; i < bArray.length; ++i) {
                String sTemp = Integer.toHexString(255 & bArray[i]);
                if (sTemp.length() < 2) {
                    sb.append(0);
                }

                sb.append(sTemp.toUpperCase());
            }

            return sb.toString();
        }
    }

    public static Object getSerializableObject(Context context, String fileName, String key) {
        try {
            SharedPreferences sp = context.getSharedPreferences(fileName, 0);
            if (sp.contains(key)) {
                String string = sp.getString(key, "");
                if (TextUtils.isEmpty(string)) {
                    return null;
                } else {
                    byte[] stringToBytes = StringToBytes(string);
                    ByteArrayInputStream bis = new ByteArrayInputStream(stringToBytes);
                    ObjectInputStream is = new ObjectInputStream(bis);
                    Object readObject = is.readObject();
                    return readObject;
                }
            } else {
                return null;
            }
        } catch (Exception var9) {
            return null;
        }
    }

    public static byte[] StringToBytes(String data) {
        String hexString = data.toUpperCase().trim();
        if (hexString.length() % 2 != 0) {
            return null;
        } else {
            byte[] retData = new byte[hexString.length() / 2];

            for (int i = 0; i < hexString.length(); ++i) {
                char hex_char1 = hexString.charAt(i);
                int int_ch1;
                if (hex_char1 >= '0' && hex_char1 <= '9') {
                    int_ch1 = (hex_char1 - 48) * 16;
                } else {
                    if (hex_char1 < 'A' || hex_char1 > 'F') {
                        return null;
                    }

                    int_ch1 = (hex_char1 - 55) * 16;
                }

                ++i;
                char hex_char2 = hexString.charAt(i);
                int int_ch2;
                if (hex_char2 >= '0' && hex_char2 <= '9') {
                    int_ch2 = hex_char2 - 48;
                } else {
                    if (hex_char2 < 'A' || hex_char2 > 'F') {
                        return null;
                    }

                    int_ch2 = hex_char2 - 55;
                }

                int int_ch = int_ch1 + int_ch2;
                retData[i / 2] = (byte) int_ch;
            }

            return retData;
        }
    }


}
